package com.example.huazong.myapplication;

/**
 * Created by huazong on 2021/6/10.
 */

import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder>{

    private List<ChatMsg> list;
    public MsgAdapter(List<ChatMsg> list){
        this.list = list;
    }




    //public void remove(int position) {          //撤回
    //    list.remove(position);
    //    notifyItemRemoved(position);            //刷新
    //    notifyItemRangeChanged(position,getItemCount());
    //}
    public void clearing(){        //清空
        list.clear();
        notifyDataSetChanged();    //刷新
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout leftLayout;
        TextView left_msg;
        TextView now_time;
        LinearLayout rightLayout;
        TextView right_msg,current_time;

        @RequiresApi(api = Build.VERSION_CODES.N)
        public ViewHolder(View view){
            super(view);
            leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
            left_msg = (TextView) view.findViewById(R.id.left_msg);
            current_time = (TextView) view.findViewById(R.id.now_time);
            rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
            right_msg = (TextView) view.findViewById(R.id.right_msg);
            now_time = (TextView) view.findViewById(R.id.now_time);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
//获取当前时间
            Date date = new Date(System.currentTimeMillis());
            now_time.setText(simpleDateFormat.format(date));

        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ChatMsg chatMsg = list.get(position);
        if(chatMsg.getType() == ChatMsg.TYPE_RECEIVED){
            //如果是收到的消息，则显示左边的消息布局，将右边的消息布局隐藏
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.left_msg.setText(chatMsg.getContent());

            //注意此处隐藏右面的消息布局用的是 View.GONE
            holder.rightLayout.setVisibility(View.GONE);
        }else if(chatMsg.getType() == ChatMsg.TYPE_SEND){
            //如果是发出的消息，则显示右边的消息布局，将左边的消息布局隐藏
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.right_msg.setText(chatMsg.getContent());

            //同样使用View.GONE
            holder.leftLayout.setVisibility(View.GONE);

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }




}
